<?php 
/**
 * Main plugin file. This plugin adds useful admin links and resources for BuddyPress to the WordPress Toolbar / Admin Bar.
 *
 * @package   BuddyPress Toolbar
 * @author    David Decker
 * @link      http://twitter.com/#!/deckerweb
 * @copyright Copyright 2012, David Decker - DECKERWEB
 *
 * @credits   Inspired and based on the plugin "WooThemes Admin Bar Addition" by Remkus de Vries @defries.
 * @link      http://remkusdevries.com/
 * @link      http://twitter.com/#!/defries
 *
 * Plugin Name: BuddyPress Toolbar
 * Plugin URI: http://genesisthemes.de/en/wp-plugins/buddypress-toolbar/
 * Description: This plugin adds useful admin links and resources for BuddyPress to the WordPress Toolbar / Admin Bar.
 * Version: 1.1
 * Author: David Decker - DECKERWEB
 * Author URI: http://deckerweb.de/
 * License: GPLv2
 * Text Domain: buddypress-toolbar
 * Domain Path: /languages/
 */

/**
 * Setting constants
 *
 * @since 1.0
 */
define( 'BPTB_PLUGIN_BASEDIR', dirname( plugin_basename( __FILE__ ) ) );


add_action( 'init', 'ddw_bptb_init' );
/**
 * Load the text domain for translation of the plugin
 * 
 * @since 1.0
 */
function ddw_bptb_init() {

	load_plugin_textdomain( 'buddypress-toolbar', false, BPTB_PLUGIN_BASEDIR . '/languages/' );
}


add_filter( 'plugin_row_meta', 'ddw_bptb_plugin_links', 10, 2 );
/**
 * Add various support links to plugin page
 *
 * @since 1.0
 *
 * @param  $bptb_links
 * @param  $bptb_file
 * @return strings plugin links
 */
function ddw_bptb_plugin_links( $bptb_links, $bptb_file ) {

	if ( !current_user_can( 'install_plugins' ) )
		return $bptb_links;

	if ( $bptb_file == BPTB_PLUGIN_BASEDIR . '/buddypress-toolbar.php' ) {
		$bptb_links[] = '<a href="http://wordpress.org/extend/plugins/buddypress-toolbar/faq/" target="_new" title="' . __( 'FAQ', 'buddypress-toolbar' ) . '">' . __( 'FAQ', 'buddypress-toolbar' ) . '</a>';
		$bptb_links[] = '<a href="http://wordpress.org/tags/buddypress-toolbar?forum_id=10" target="_new" title="' . __( 'Support', 'buddypress-toolbar' ) . '">' . __( 'Support', 'buddypress-toolbar' ) . '</a>';
		$bptb_links[] = '<a href="' . __( 'http://genesisthemes.de/en/donate/', 'buddypress-toolbar' ) . '" target="_new" title="' . __( 'Donate', 'buddypress-toolbar' ) . '">' . __( 'Donate', 'buddypress-toolbar' ) . '</a>';
	}

	return $bptb_links;
}


add_action( 'admin_bar_menu', 'ddw_bptb_admin_bar_menu', 98 );
/**
 * Add new menu items to the WP Admin Bar.
 * 
 * @since 1.0
 * @version 1.1
 *
 * @global mixed $wp_admin_bar 
 */
function ddw_bptb_admin_bar_menu() {

	global $wp_admin_bar;

	/**
	 * Required BuddyPress/ WordPress cabability to display new admin bar entry
	 * Only showing items if toolbar / admin bar is activated and user is logged in!
	 */
	if ( !is_user_logged_in() && ( !current_user_can( 'install_plugins' ) || !is_admin_bar_showing() ) )
		return;

	/** Set unique prefix */
	$prefix = 'ddw-buddypress-';
	
	/** Create parent menu item references */
	$bpbar = $prefix . 'admin-bar';					// root level
		$bpsupport = $prefix . 'bpsupport';				// sub level: bp support
		$bpcodex = $prefix . 'bpcodex';					// sub level: bp codex
		$bpsites = $prefix . 'bpsites';					// sub level: bp sites
		$bpactivity = $prefix . 'bpactivity';				// sub level: bp activity
		$bpsettings = $prefix . 'bpsettings';				// sub level: bp settings
		$bppfields = $prefix . 'bppfields';				// sub level: bp profile fields
		$users = $prefix . 'users';					// sub level: users
			$usersinviteanyone = $prefix . 'usersinviteanyone';		// third level: invite anyone
		$extensions = $prefix . 'extensions';				// sub level: extensions
			$extwangguard = $prefix . 'extwangguard';			// third level plugin extension: wangguard
			$extbpge = $prefix . 'extbpge';					// third level plugin extension: bp groups extras
			$extbuddystream = $prefix . 'extbuddystream';			// third level plugin extension: buddystream
			$extinviteanyone = $prefix . 'extinviteanyone';			// third level plugin extension: invite anyone
			$exteventsmanager = $prefix . 'exteventsmanager';		// third level plugin extension: events manager
		$tgroup = $prefix . 'tgroup';					// sub level: theme group ("hook" place)
			$bservicessettings = $prefix . 'bservicessettings';		// third level theme: business services
		$bpgroup = $prefix . 'bpgroup';					// sub level: bp group (resources)
	
	/** Display these items also when BuddyPress plugin is not installed */
	$bpgroup_menu_items = array(

		// Support menu items
		'bpsupport' => array(
			'parent' => $bpgroup,
			'title'  => __( 'BuddyPress Support', 'buddypress-toolbar' ),
			'href'   => 'http://buddypress.org/support/',
			'meta'   => array( 'title' => __( 'BuddyPress Support', 'buddypress-toolbar' ) )
		),
		'bpsupportwporg' => array(
			'parent' => $bpsupport,
			'title'  => __( 'WordPress.org Support Forum', 'buddypress-toolbar' ),
			'href'   => 'http://wordpress.org/tags/buddypress?forum_id=10',
			'meta'   => array( 'title' => __( 'WordPress.org Support Forum', 'buddypress-toolbar' ) )
		),

		// Codex menu items
		'bpcodex' => array(
			'parent' => $bpgroup,
			'title'  => __( 'Codex & Documentation', 'buddypress-toolbar' ),
			'href'   => 'http://codex.buddypress.org/',
			'meta'   => array( 'title' => __( 'Codex & Documentation', 'buddypress-toolbar' ) )
		),
		'bpcodex-releases' => array(
			'parent' => $bpcodex,
			'title'  => __( 'Releases', 'buddypress-toolbar' ),
			'href'   => 'http://codex.buddypress.org/releases/',
			'meta'   => array( 'title' => __( 'Releases - see in the sidebar there!', 'buddypress-toolbar' ) )
		),
		'bpcodex-themes' => array(
			'parent' => $bpcodex,
			'title'  => __( 'Theme Development', 'buddypress-toolbar' ),
			'href'   => 'http://codex.buddypress.org/theme-development/',
			'meta'   => array( 'title' => __( 'Theme Development - see in the sidebar there!', 'buddypress-toolbar' ) )
		),
		'bpcodex-developer' => array(
			'parent' => $bpcodex,
			'title'  => __( 'Developer Docs', 'buddypress-toolbar' ),
			'href'   => 'http://codex.buddypress.org/developer-docs/',
			'meta'   => array( 'title' => __( 'Developer Docs - see in the sidebar there!', 'buddypress-toolbar' ) )
		),

		// BuddyPress HQ menu items
		'bpsites' => array(
			'parent' => $bpgroup,
			'title'  => __( 'BuddyPress HQ', 'buddypress-toolbar' ),
			'href'   => 'http://buddypress.org/',
			'meta'   => array( 'title' => __( 'BuddyPress HQ', 'buddypress-toolbar' ) )
		),
		'bpblog' => array(
			'parent' => $bpsites,
			'title'  => __( 'Official Blog', 'buddypress-toolbar' ),
			'href'   => 'http://buddypress.org/blog/',
			'meta'   => array( 'title' => __( 'Official Blog', 'buddypress-toolbar' ) )
		),
		'bpabout' => array(
			'parent' => $bpsites,
			'title'  => __( 'About BuddyPress', 'buddypress-toolbar' ),
			'href'   => 'http://buddypress.org/about/',
			'meta'   => array( 'title' => __( 'About BuddyPress', 'buddypress-toolbar' ) )
		),
		'bpcommunity' => array(
			'parent' => $bpsites,
			'title'  => __( 'BuddyPress Community', 'buddypress-toolbar' ),
			'href'   => 'http://buddypress.org/community/',
			'meta'   => array( 'title' => __( 'BuddyPress Community', 'buddypress-toolbar' ) )
		),
		'bpdevel' => array(
			'parent' => $bpsites,
			'title'  => __( 'Development Updates', 'buddypress-toolbar' ),
			'href'   => 'http://bpdevel.wordpress.com/',
			'meta'   => array( 'title' => __( 'Development Updates', 'buddypress-toolbar' ) )
		),
		'bptrac' => array(
			'parent' => $bpsites,
			'title'  => __( 'Trac: Tickets &amp; Bug Reports', 'buddypress-toolbar' ),
			'href'   => 'http://buddypress.trac.wordpress.org/roadmap',
			'meta'   => array( 'title' => __( 'Trac: Tickets &amp; Bug Reports', 'buddypress-toolbar' ) )
		),
		'bpextendplugins' => array(
			'parent' => $bpsites,
			'title'  => __( 'Extend BuddyPress: Plugins', 'buddypress-toolbar' ),
			'href'   => 'http://buddypress.org/extend/plugins/',
			'meta'   => array( 'title' => __( 'Extend BuddyPress: Plugins', 'buddypress-toolbar' ) )
		),
		'bpextendrecplug' => array(
			'parent' => $bpsites,
			'title'  => __( 'Extend BuddyPress: Recommended Plugins', 'buddypress-toolbar' ),
			'href'   => 'http://buddypress.org/extend/recommended-plugins/',
			'meta'   => array( 'title' => __( 'Extend BuddyPress: Recommended Plugins', 'buddypress-toolbar' ) )
		),
		'bpextendthemes' => array(
			'parent' => $bpsites,
			'title'  => __( 'Extend BuddyPress: Themes', 'buddypress-toolbar' ),
			'href'   => 'http://buddypress.org/extend/themes/',
			'meta'   => array( 'title' => __( 'Extend BuddyPress: Themes', 'buddypress-toolbar' ) )
		),
		'bpluginswporg' => array(
			'parent' => $bpsites,
			'title'  => __( 'More free plugins/extensions at WP.org', 'buddypress-toolbar' ),
			'href'   => 'http://wordpress.org/extend/plugins/tags/buddypress/',
			'meta'   => array( 'title' => __( 'More free plugins/extensions at WP.org', 'buddypress-toolbar' ) )
		),
		'bpthemeswporg' => array(
			'parent' => $bpsites,
			'title'  => __( 'More free Themes at WP.org', 'buddypress-toolbar' ),
			'href'   => 'http://wordpress.org/extend/themes/tags/buddypress',
			'meta'   => array( 'title' => __( 'More free Themes at WP.org', 'buddypress-toolbar' ) )
		),
		'bptrickscom' => array(
			'parent' => $bpsites,
			'title'  => __( 'BP-Tricks.com (Community Site)', 'buddypress-toolbar' ),
			'href'   => 'http://bp-tricks.com/',
			'meta'   => array( 'title' => __( 'BP-Tricks.com - A BuddyPress Community Site', 'buddypress-toolbar' ) )
		),
		'bpffnews' => array(
			'parent' => $bpsites,
			'title'  => __( 'BuddyPress News Planet', 'buddypress-toolbar' ),
			'href'   => 'http://friendfeed.com/buddypress-news',
			'meta'   => array( 'title' => __( 'BuddyPress News Planet (official and community news via FriendFeed service)', 'buddypress-toolbar' ) )
		),
	);


	/** Display language specific links only for these locales: de_DE, de_AT, de_CH, de_LU */
	if ( get_locale() == 'de_DE' || get_locale() == 'de_AT' || get_locale() == 'de_CH' || get_locale() == 'de_LU' ) {
		// German BP forum
		$bpgroup_menu_items['bp-forum-de'] = array(
			'parent' => $bpgroup,
			'title'  => __( 'German Support Forum', 'buddypress-toolbar' ),
			'href'   => 'http://forum.wpde.org/buddypress/',
			'meta'   => array( 'title' => __( 'German Support Forum', 'buddypress-toolbar' ) )
		);

		// German BP language packs
		$bpgroup_menu_items['bp-languages-de'] = array(
			'parent' => $bpgroup,
			'title'  => __( 'German language files', 'buddypress-toolbar' ),
			'href'   => 'http://deckerweb.de/material/sprachdateien/buddypress/',
			'meta'   => array( 'title' => __( 'German language files for BuddyPress and more BP extensions', 'buddypress-toolbar' ) )
		);
	}  // end-if German locales


	/** Check for active BP version to define settings links */
	if ( class_exists( 'BuddyPress' ) ) {
		$bp_aurl_main = admin_url( 'options-general.php?page=bp-components' );
		$bp_aurl_pfields = admin_url( 'users.php?page=bp-profile-setup' );
	} else {
		$bp_aurl_main = admin_url( 'admin.php?page=bp-general-config' );
		$bp_aurl_pfields = admin_url( 'admin.php?page=bp-profile-setup' );
	}  // end-if BP check


	/** Show these items only if BuddyPress plugin is actually installed */
	if ( ( function_exists( 'is_plugin_active' ) && is_plugin_active( 'buddypress/bp-loader.php' ) ) || class_exists( 'BP_Core' ) ) {

		// Check for BP v1.6+ - if true, display "Moderate Activity" link
		if ( class_exists( 'BuddyPress' ) ) {
			$menu_items['bpactivity'] = array(
				'parent' => $bpbar,
				'title'  => __( 'Moderate Activity', 'buddypress-toolbar' ),
				'href'   => admin_url( 'admin.php?page=bp-activity' ),
				'meta'   => array( 'target' => '', 'title' => __( 'Moderate Activity', 'buddypress-toolbar' ) )

			);
		}  // end-if BP moderate activity check

		// BP Settings section
		$menu_items['bpsettings'] = array(
			'parent' => $bpbar,
			'title'  => __( 'BuddyPress Settings', 'buddypress-toolbar' ),
			'href'   => $bp_aurl_main,
			'meta'   => array( 'target' => '', 'title' => __( 'BuddyPress Settings', 'buddypress-toolbar' ) )
		);
		$menu_items['s-pages'] = array(
			'parent' => $bpsettings,
			'title'  => __( 'Pages for Components', 'buddypress-toolbar' ),
			'href'   => admin_url( 'admin.php?page=bp-page-settings' ),
			'meta'   => array( 'target' => '', 'title' => __( 'Pages for Components', 'buddypress-toolbar' ) )
		);
		$menu_items['s-moresettings'] = array(
			'parent' => $bpsettings,
			'title'  => __( 'Community &amp; Groups', 'buddypress-toolbar' ),
			'href'   => admin_url( 'admin.php?page=bp-settings' ),
			'meta'   => array( 'target' => '', 'title' => __( 'Community &amp; Groups', 'buddypress-toolbar' ) )
		);
		$menu_items['s-forums'] = array(
			'parent' => $bpsettings,
			'title'  => __( 'Forums Setup', 'buddypress-toolbar' ),
			'href'   => admin_url( 'admin.php?page=bb-forums-setup' ),
			'meta'   => array( 'target' => '', 'title' => __( 'Forums Setup', 'buddypress-toolbar' ) )
		);
		$menu_items['s-bpwidgets'] = array(
			'parent' => $bpsettings,
			'title'  => __( 'BuddyPress Widgets', 'buddypress-toolbar' ),
			'href'   => admin_url( 'widgets.php' ),
			'meta'   => array( 'target' => '', 'title' => __( 'BuddyPress Widgets', 'buddypress-toolbar' ) )
		);

		// Profile Fields section
		$menu_items['bppfields'] = array(
			'parent' => $bpbar,
			'title'  => __( 'Profile Fields/Groups', 'buddypress-toolbar' ),
			'href'   => $bp_aurl_pfields,
			'meta'   => array( 'target' => '', 'title' => __( 'Profile Fields/Groups', 'buddypress-toolbar' ) )
		);
		$menu_items['bppfields-add'] = array(
			'parent' => $bppfields,
			'title'  => __( 'Add new Field Group', 'buddypress-toolbar' ),
			'href'   => admin_url( 'admin.php?page=bp-profile-setup&mode=add_group' ),
			'meta'   => array( 'target' => '', 'title' => __( 'Add new Field Group', 'buddypress-toolbar' ) )
		);

		// Users section
		if ( current_user_can( 'edit_users' ) ) {
			$menu_items['users'] = array(
				'parent' => $bpbar,
				'title'  => __( 'Users', 'buddypress-toolbar' ),
				'href'   => admin_url( 'users.php' ),
				'meta'   => array( 'target' => '', 'title' => __( 'Users', 'buddypress-toolbar' ) )
			);
		}  // end-if can edit_users
		if ( current_user_can( 'add_users' ) ) {
			$menu_items['u-add-user'] = array(
				'parent' => $users,
				'title'  => __( 'Add new User', 'buddypress-toolbar' ),
				'href'   => admin_url( 'user-new.php' ),
				'meta'   => array( 'target' => '', 'title' => __( 'Add new User', 'buddypress-toolbar' ) )
			);
		}  // end-if can add_users

		// Check for active bbPress 2.x forums - if true, display settings link
		if ( class_exists( 'bbPress' ) && !function_exists( 'ddw_bbpress_aba_admin_bar_menu' ) ) {
			$menu_items['bpbbpress'] = array(
				'parent' => $bpbar,
				'title'  => __( 'bbPress Forums', 'buddypress-toolbar' ),
				'href'   => admin_url( 'options-general.php?page=bbpress' ),
				'meta'   => array( 'target' => '', 'title' => __( 'bbPress Forums', 'buddypress-toolbar' ) )
			);
		}

		/**
		 * Display last main item in the menu for active extensions/plugins
		 * ATTENTION: This is where plugins/extensions hook in on the sub-level hierarchy
		 *
		 * @since 1.0
		 */
		$menu_items['extensions'] = array(
			'parent' => $bpbar,
			'title'  => __( 'Active Extensions', 'buddypress-toolbar' ),
			'href'   => admin_url( 'plugins.php' ),
			'meta'   => array( 'target' => '', 'title' => __( 'Active Extensions', 'buddypress-toolbar' ) )
		);

	}  // end-if BP conditional


	/**
	 * Display links to active BuddyPress specific themes settings' pages
	 *
	 * @since 1.0
	 */
		/** Include plugin file with theme support links */
		require_once( 'bptb-themes.php' );


	/**
	 * Display links to active BuddyPress specific plugins/extensions settings' pages
	 *
	 * @since 1.0
	 */
		/** Include plugin file with plugin support links */
		require_once( 'bptb-plugins.php' );


	/** Allow menu items to be filtered, but pass in parent menu item IDs */
	$menu_items = (array) apply_filters( 'ddw_bptb_menu_items', $menu_items, $bpgroup_menu_items, $prefix, $bpbar, $bpsupport, $bpcodex, $bpsites, $bpactivity, $bpsettings, $users, $usersinviteanyone, $bppfields, $extensions, $extwangguard, $extbpge, $extbuddystream, $extinviteanyone, $exteventsmanager, $tgroup, $bservicessettings, $bpgroup );


	/** Add the BuddyPress top-level menu item */
	$wp_admin_bar->add_menu( array(
		'id'    => $bpbar,
		'title' => __( 'BuddyPress', 'buddypress-toolbar' ),
		'href'  => $bp_aurl_main,
		'meta'  => array( 'class' => 'icon-buddypress', 'title' => __( 'BuddyPress', 'buddypress-toolbar' ) )
	) );


	/** Loop through the menu items */
	foreach ( $menu_items as $id => $menu_item ) {
		
		// Add in the item ID
		$menu_item['id'] = $prefix . $id;

		// Add meta target to each item where it's not already set, so links open in new window/tab
		if ( ! isset( $menu_item['meta']['target'] ) )		
			$menu_item['meta']['target'] = '_blank';

		// Add class to links that open up in a new window/tab
		if ( '_blank' === $menu_item['meta']['target'] ) {
			if ( ! isset( $menu_item['meta']['class'] ) )
				$menu_item['meta']['class'] = '';
			$menu_item['meta']['class'] .= $prefix . 'bptb-new-tab';
		}

		// Add item
		$wp_admin_bar->add_menu( $menu_item );

	}  // end foreach menu items


	/** Theme Group: Main Entry */
	$wp_admin_bar->add_group( array(
		'parent' => $bpbar,
		'id'     => $tgroup,
	) );


	/** BP Group: Main Entry */
	$wp_admin_bar->add_group( array(
		'parent' => $bpbar,
		'id'     => $bpgroup,
		'meta'   => array( 'class' => 'ab-sub-secondary' )
	) );


	/** BP Group: Loop through the group menu items */
	foreach ( $bpgroup_menu_items as $id => $bpgroup_menu_item ) {
		
		// BP Group: Add in the item ID
		$bpgroup_menu_item['id'] = $prefix . $id;

		// BP Group: Add meta target to each item where it's not already set, so links open in new window/tab
		if ( ! isset( $bpgroup_menu_item['meta']['target'] ) )		
			$bpgroup_menu_item['meta']['target'] = '_blank';

		// BP Group: Add class to links that open up in a new window/tab
		if ( '_blank' === $bpgroup_menu_item['meta']['target'] ) {
			if ( ! isset( $bpgroup_menu_item['meta']['class'] ) )
				$bpgroup_menu_item['meta']['class'] = '';
			$bpgroup_menu_item['meta']['class'] .= $prefix . 'bptb-new-tab';
		}

		// BP Group: Add item
		$wp_admin_bar->add_menu( $bpgroup_menu_item );

	}  // end foreach BP Group

}  // end of main function


add_action( 'wp_head', 'ddw_bptb_admin_style' );
add_action( 'admin_head', 'ddw_bptb_admin_style' );
/**
 * Add the styles for new WP Admin Bar entry
 * 
 * @since 1.0
 */
function ddw_bptb_admin_style() {

	/** No styles if admin bar is disabled or user is not logged in */
	if ( !is_admin_bar_showing() || !is_user_logged_in() )
		return;

	/** Add CSS styles to wp_head/admin_head */
	?>
	<style type="text/css">
		#wpadminbar.nojs .ab-top-menu > li.menupop.icon-buddypress:hover > .ab-item,
		#wpadminbar .ab-top-menu > li.menupop.icon-buddypress.hover > .ab-item,
		#wpadminbar.nojs .ab-top-menu > li.menupop.icon-buddypress > .ab-item,
		#wpadminbar .ab-top-menu > li.menupop.icon-buddypress > .ab-item {
      			background-image: url(<?php echo plugins_url( 'buddypress/bp-core/images/admin_menu_icon.png',
dirname( __FILE__ ) ); ?>);
			background-repeat: no-repeat;
			background-position: -1px 0;
			padding-left: 25px;
		}
		#wp-admin-bar-ddw-buddypress-extensions {
    			border-top: 1px solid;
			margin-bottom: 5px;
			margin-top: 5px;
		}
		#wp-admin-bar-ddw-buddypress-bp-forum-de > .ab-item:before,
		#wp-admin-bar-ddw-buddypress-bp-languages-de > .ab-item:before {
			color: #ff9900;
			content: '• ';
		}
		#wp-admin-bar-ddw-buddypress-tgroup {
			border-top: 0 none !important;
			margin-bottom: -5px !important;
			margin-top: -10px !important;
		}
	</style>
	<?php

}  // end of function ddw_bptb_admin_style
